'''
Author: 神仙盼盼 3027925806@qq.com
Date: 2024-03-07 12:51:29
LastEditors: 神仙盼盼 3027925806@qq.com
LastEditTime: 2024-03-07 13:00:03
FilePath: \Leetcode\day8\112.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
        return self.add_value(root, targetSum, 0)

    def add_value(self, root, targetSum, total_sum):
        if root is None:
            return False
        elif root.left is None and root.right is None:
            return total_sum + root.val == targetSum
        else:
            return self.add_value(root.left, targetSum, total_sum + root.val) or self.add_value(root.right, targetSum, total_sum + root.val)